home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / libs / garshnelib37 / garshnelib.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  13KB  |  487 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <graphics/gfxmacros.h>
  11. #include <graphics/videocontrol.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <libraries/reqtools.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/asl_protos.h>
  19. #include <clib/reqtools_protos.h>
  20. #include <clib/alib_protos.h>
  21.  
  22. #include <pragmas/exec_pragmas.h>
  23. #include <pragmas/graphics_pragmas.h>
  24. #include <pragmas/intuition_pragmas.h>
  25. #include <pragmas/asl_pragmas.h>
  26. #include <pragmas/reqtools_pragmas.h>
  27.  
  28. #include "Garshnelib_protos.h"
  29. #include "Garshnelib_rev.h"
  30.  
  31. STATIC const UBYTE VersTag[] = VERSTAG;
  32.  
  33. VOID SASM ScreenModeRequest( AREG(0) struct Window *Wnd,
  34.                             AREG(1) LONG *Mode, AREG(2) LONG *Depth )
  35. {
  36.     struct AslBase *AslBase;
  37.     
  38.     if( AslBase = ( struct AslBase * )OpenLibrary( AslName, 38L ))
  39.     {
  40.         struct ScreenModeRequester *smRequest;
  41.         
  42.         if( smRequest = AllocAslRequestTags( ASL_ScreenModeRequest, TAG_END ))
  43.         {
  44.             if( Depth )
  45.             {
  46.                 if( AslRequestTags( smRequest, ASLSM_Window, Wnd,
  47.                                    ASLSM_SleepWindow, TRUE,
  48.                                    ASLSM_TitleText, "Screen Mode",
  49.                                    ASLSM_InitialDisplayID, *Mode,
  50.                                    ASLSM_InitialDisplayDepth, *Depth,
  51.                                    ASLSM_DoDepth, TRUE, TAG_END ))
  52.                 {
  53.                     *Mode = smRequest->sm_DisplayID;
  54.                     *Depth = smRequest->sm_DisplayDepth;
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 if( AslRequestTags( smRequest, ASLSM_Window, Wnd,
  60.                                    ASLSM_SleepWindow, TRUE,
  61.                                    ASLSM_TitleText, "Screen Mode",
  62.                                    ASLSM_InitialDisplayID, *Mode, TAG_END ))
  63.                     *Mode = smRequest->sm_DisplayID;
  64.             }
  65.             FreeAslRequest( smRequest );
  66.         }
  67.         CloseLibrary(( struct Library * )AslBase );
  68.     }
  69.     else
  70.     {
  71.         struct ReqToolsBase *ReqToolsBase;
  72.  
  73.         if( ReqToolsBase = ( struct ReqToolsBase * )
  74.            OpenLibrary( REQTOOLSNAME, REQTOOLSVERSION ))
  75.         {
  76.             struct rtScreenModeRequester *smRequest;
  77.             
  78.             if( smRequest = rtAllocRequestA( RT_SCREENMODEREQ, 0L ))
  79.             {
  80.                 if( Depth )
  81.                 {
  82.                     LONG Tags[] = { RT_Window, 0L, RT_LockWindow, TRUE,
  83.                                         RTSC_Flags, SCREQF_DEPTHGAD, TAG_END };
  84.                     LONG ReqTags[] = { RTSC_DisplayID, 0L,
  85.                                            RTSC_DisplayDepth, 0L, TAG_END };
  86.                     ReqTags[1] = *Mode;
  87.                     ReqTags[3] = *Depth;
  88.                     rtChangeReqAttrA( smRequest, ( struct TagItem * )ReqTags );
  89.                     
  90.                     Tags[1] = ( LONG )Wnd;
  91.                     if( rtScreenModeRequestA( smRequest, "Screen Mode",
  92.                                              ( struct TagItem * )Tags ))
  93.                     {
  94.                         *Mode = smRequest->DisplayID;
  95.                         *Depth = smRequest->DisplayDepth;
  96.                     }
  97.                 }
  98.                 else
  99.                 {
  100.                     LONG Tags[] = { RT_Window, 0L, RT_LockWindow, TRUE,
  101.                                         TAG_END };
  102.                     LONG ReqTags[] = { RTSC_DisplayID, 0L, TAG_END };
  103.                     
  104.                     ReqTags[1] = *Mode;
  105.                     rtChangeReqAttrA( smRequest, ( struct TagItem * )ReqTags );
  106.                     
  107.                     Tags[1] = ( LONG )Wnd;
  108.                     if( rtScreenModeRequestA( smRequest, "Screen Mode",
  109.                                              ( struct TagItem * )Tags ))
  110.                         *Mode = smRequest->DisplayID;
  111.                 }
  112.                 rtFreeRequest( smRequest );
  113.             }
  114.             CloseLibrary(( struct Library * )ReqToolsBase );
  115.         }
  116.     }
  117. }
  118.  
  119. #define NUMCOLORS 45
  120. LONG spectrum[] = {
  121.     0x0F00, 0x0E10, 0x0D20, 0x0C30, 0x0B40, 0x0A50, 0x0960,    0x0870, 0x0780,
  122.     0x0690, 0x05A0, 0x04B0, 0x03C0, 0x02D0,    0x01E0, 0x00F0, 0x00E1, 0x00D2,
  123.     0x00C3, 0x00B4, 0x00A5,    0x0096, 0x0087, 0x0078, 0x0069, 0x005A, 0x004B,
  124.     0x003C,    0x002D, 0x001E, 0x000F, 0x010E, 0x020D, 0x030C, 0x040B,    0x050A,
  125.     0x0609, 0x0708, 0x0807, 0x0906, 0x0A05, 0x0B04,    0x0C03, 0x0D02, 0x0E01
  126.     };
  127.  
  128. VOID SASM setCopperList( DREG(0) LONG Hei, DREG(1) LONG Col,
  129.                         AREG(0) struct ViewPort *VPort,
  130.                         AREG(1) struct Custom *Custom )
  131. {
  132.     struct Library *GfxBase, *IntuitionBase;
  133.     struct UCopList *uCopList;
  134.  
  135.     IntuitionBase = OpenLibrary( "intuition.library", 37L );
  136.     GfxBase = OpenLibrary( "graphics.library", 37L );
  137.     
  138.     if( IntuitionBase && GfxBase &&
  139.        ( uCopList = AllocVec( sizeof( struct UCopList ), MEMF_CLEAR )))
  140.     {
  141.         struct TagItem uCopTags[] = {{VTAG_USERCLIP_SET,0L},{VTAG_END_CM,0L}};
  142.         LONG i, spc;
  143.  
  144.         spc = Hei/NUMCOLORS;
  145.         CINIT( uCopList, NUMCOLORS );
  146.         for( i = 0; i < NUMCOLORS; ++i )
  147.         {
  148.             CWAIT( uCopList, i * spc, 0 );
  149.             CMOVE( uCopList, Custom->color[Col], spectrum[i%NUMCOLORS] );
  150.         }
  151.         CEND( uCopList );
  152.         Forbid();
  153.         VPort->UCopIns = uCopList;
  154.         Permit();
  155.         VideoControl( VPort->ColorMap, uCopTags );
  156.         RethinkDisplay();
  157.     }
  158.     
  159.     if( GfxBase )
  160.         CloseLibrary( GfxBase );
  161.     
  162.     if( IntuitionBase )
  163.         CloseLibrary( IntuitionBase );
  164. }
  165.  
  166. VOID SASM clearCopperList( AREG(0) struct ViewPort *VPort )
  167. {
  168.     struct Library *GfxBase;
  169.     
  170.     if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  171.     {
  172.         struct TagItem uCopTags[] = {{VTAG_USERCLIP_CLR,0L},{VTAG_END_CM,0L}};
  173.         struct UCopList *uCopList;
  174.  
  175.         VideoControl( VPort->ColorMap, uCopTags );
  176.         Forbid();
  177.         uCopList = VPort->UCopIns;
  178.         VPort->UCopIns = 0L;
  179.         Permit();
  180.         FreeVec( uCopList );
  181.     }
  182. }
  183.  
  184. LONG SASM getTopScreenMode( VOID )
  185. {
  186.     struct IntuitionBase *IntuitionBase;
  187.     struct Library *GfxBase;
  188.     struct Screen *pubScr;
  189.     LONG scrMode;
  190.     BPTR lock;
  191.     
  192.     IntuitionBase =
  193.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L );
  194.     GfxBase = OpenLibrary( "graphics.library", 37L );
  195.     
  196.     if( IntuitionBase && GfxBase )
  197.     {
  198.         lock = LockIBase( 0 );
  199.         pubScr = IntuitionBase->FirstScreen;
  200.         scrMode = GetVPModeID(&( pubScr->ViewPort));
  201.         UnlockIBase( lock );
  202.     }
  203.     
  204.     if( GfxBase )
  205.         CloseLibrary( GfxBase );
  206.     
  207.     if( IntuitionBase )
  208.         CloseLibrary(( struct Library * )IntuitionBase );
  209.     
  210.     return scrMode;
  211. }
  212.  
  213. LONG SASM getTopScreenDepth( VOID )
  214. {
  215.     struct IntuitionBase *IntuitionBase;
  216.     LONG Dep = 0;
  217.     
  218.     if( IntuitionBase =
  219.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L ))
  220.     {
  221.         struct Screen *PubScr;
  222.         struct DrawInfo *dri;
  223.         BPTR Lock;
  224.  
  225.         Lock = LockIBase( 0 );
  226.         PubScr = IntuitionBase->FirstScreen;
  227.         if( dri = GetScreenDrawInfo( PubScr ))
  228.         {
  229.             Dep = dri->dri_Depth;
  230.             FreeScreenDrawInfo( PubScr, dri );
  231.         }
  232.         UnlockIBase( Lock );
  233.     
  234.         CloseLibrary(( struct Library * )IntuitionBase );
  235.     }
  236.     
  237.     return Dep;
  238. }
  239.  
  240. struct Screen *SASM cloneTopScreen( DREG(0) LONG MoreColors,
  241.                                    DREG(1) LONG GetPublic )
  242. {
  243.     LONG sMod, sDep, i, Wid, Hei, offx, offy;
  244.     struct IntuitionBase *IntuitionBase;
  245.     struct Screen *Scr, *nScr = 0L;
  246.     struct Rectangle DispRect;
  247.     struct Library *GfxBase;
  248.     struct DrawInfo *dri;
  249.     UWORD *cols;
  250.     BPTR lock;
  251.     
  252.     IntuitionBase =
  253.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L );
  254.     GfxBase = OpenLibrary( "graphics.library", 37L );
  255.     
  256.     if( IntuitionBase && GfxBase )
  257.     {
  258.         /* Lock IntuitionBase so nothing goes away */
  259.         lock = LockIBase( 0 );
  260.         
  261.         /* Grab the first screen and get its attributes */
  262.         if( GetPublic )
  263.             Scr = LockPubScreen( 0L );
  264.         else
  265.             Scr = IntuitionBase->FirstScreen;
  266.         sMod = GetVPModeID(&( Scr->ViewPort )); /* Screen Mode ID */
  267.         offx = Scr->LeftEdge; 
  268.         offy = Scr->TopEdge;
  269.         Wid = Scr->Width;
  270.         Hei = Scr->Height;
  271.         if( dri = GetScreenDrawInfo( Scr ))
  272.             sDep = MoreColors ? dri->dri_Depth + 1 : dri->dri_Depth;
  273.         if( cols = AllocVec( sizeof( WORD ) * ( 1L << sDep ), MEMF_CLEAR ))
  274.             for( i = 0; i < ( 1L << dri->dri_Depth ); ++i )
  275.                 cols[i] = GetRGB4( Scr->ViewPort.ColorMap, i );
  276.         
  277.         if( GetPublic )
  278.             UnlockPubScreen( 0L, Scr );
  279.         
  280.         UnlockIBase( lock );
  281.         
  282.         QueryOverscan( sMod, &DispRect, OSCAN_TEXT );
  283.         
  284.         Wid = min( Wid, DispRect.MaxX - DispRect.MinX + 1 );
  285.         Hei = min( Hei, DispRect.MaxY - DispRect.MinY + 1 );
  286.         
  287.         if( sMod != INVALID_ID )
  288.         {
  289.             nScr = OpenScreenTags( NULL, SA_DisplayID, sMod, SA_Depth, sDep,
  290.                                   SA_Width, Wid, SA_Height, Hei,
  291.                                   SA_Behind, TRUE, SA_Quiet, TRUE, TAG_DONE );
  292.             if( nScr )
  293.             {
  294.                 BltBitMap( Scr->RastPort.BitMap, offx < 0 ? -offx : 0,
  295.                           offy < 0 ? -offy : 0, nScr->RastPort.BitMap, 0, 0,
  296.                           Wid, Hei, 0x00C0, 0x00FF, NULL );
  297.                 LoadRGB4( &(nScr->ViewPort), cols, 1L << sDep );
  298.                 if( offx > 0 )
  299.                     MoveScreen( nScr, offx, 0 );
  300.                 WaitBlit();
  301.                 ScreenToFront( nScr );
  302.             }
  303.         }
  304.         
  305.         if( cols )
  306.             FreeVec( cols );
  307.         
  308.         if( dri )
  309.             FreeScreenDrawInfo( Scr, dri );
  310.     }
  311.     
  312.     if( GfxBase )
  313.         CloseLibrary( GfxBase );
  314.     
  315.     if( IntuitionBase )
  316.         CloseLibrary(( struct Library * )IntuitionBase );
  317.     
  318.     return nScr;
  319. }
  320.  
  321. ULONG *SASM GetColorTable( AREG(0) struct Screen *Screen )
  322. {
  323.     return 0L;
  324. }
  325.  
  326. LONG SASM AvgBitsPerGun( DREG(0) LONG ModeID )
  327. {
  328.     return 4L;
  329. }
  330.  
  331. VOID SASM FadeAndLoadTable( AREG(0) struct Screen *Screen, DREG(0) LONG BPG,
  332.                            AREG(1) ULONG *ColorTable, DREG(1) LONG SavePlanes )
  333. {
  334.     struct Library *GfxBase;
  335.     
  336.     if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  337.     {
  338.         LONG NumCols = ( 1L << Screen->RastPort.BitMap->Depth - SavePlanes );
  339.         LONG i, col;
  340.         
  341.         for( i = 0; i < NumCols; ++i )
  342.         {
  343.             col = GetRGB4( Screen->ViewPort.ColorMap, i );
  344.             SetRGB4(&( Screen->ViewPort ), i,
  345.                     (( col & 0x0F00 ) >> 8 ) > 0 ?
  346.                     (( col & 0x0F00 ) >> 8 ) - 1 : 0,
  347.                     (( col & 0x00F0 ) >> 4 ) > 0 ?
  348.                     (( col & 0x00F0 ) >> 4 ) - 1 : 0,
  349.                     ( col & 0x000F ) > 0 ? ( col & 0x000F ) - 1 : 0 );
  350.         }
  351.     }
  352.     
  353.     if( GfxBase )
  354.         CloseLibrary( GfxBase );
  355. }
  356.  
  357. struct Window *SASM BlankMousePointer( AREG(0) struct Screen *Scr )
  358. {
  359.     struct Library *IntuitionBase, *GfxBase;
  360.     struct Window *Wnd;
  361.     ULONG oldmodes;
  362.     
  363.     IntuitionBase = OpenLibrary( "intuition.library", 37L );
  364.     GfxBase = OpenLibrary( "graphics.library", 37L );
  365.     
  366.     if( IntuitionBase && GfxBase )
  367.     {
  368.         oldmodes = SetPubScreenModes( 0 );
  369.         if( Wnd = OpenWindowTags( 0L, WA_Activate, TRUE, WA_Left, 0, WA_Top, 0,
  370.                                  WA_Width, 1, WA_Height, 1, WA_Borderless,
  371.                                  TRUE, WA_CustomScreen, Scr, TAG_END ))
  372.         {
  373.             if( Wnd->UserData = AllocVec( 3 * 2 * sizeof( UWORD ),
  374.                                          MEMF_CHIP|MEMF_CLEAR ))
  375.             {
  376.                 /* Set a pointer sprite of size 16*1 (alloc and clear data for
  377.                    sprite def which is 2 control words, 2 words 4-color data
  378.                    for each line and 2 empty words at the end (as suggested in
  379.                    Hardware Reference Manual)). */
  380.                 SetPointer( Wnd, ( UWORD * )Wnd->UserData, 1, 16, 0, 0 );
  381.             }
  382.         }
  383.         SetPubScreenModes( oldmodes );
  384.     }
  385.     
  386.     if( GfxBase )
  387.         CloseLibrary( GfxBase );
  388.     
  389.     if( IntuitionBase )
  390.         CloseLibrary( IntuitionBase );
  391.  
  392.     return Wnd;
  393. }
  394.  
  395. VOID SASM UnblankMousePointer( AREG(0) struct Window *Wnd )
  396. {
  397.     struct Library *IntuitionBase;
  398.     
  399.     if( IntuitionBase = OpenLibrary( "intuition.library", 37L ))
  400.     {
  401.         if( Wnd )
  402.         {
  403.             if( Wnd->UserData )
  404.             {
  405.                 ClearPointer( Wnd );
  406.                 FreeVec( Wnd->UserData );
  407.             }
  408.             CloseWindow( Wnd );
  409.         }
  410.         CloseLibrary( IntuitionBase );
  411.     }
  412. }
  413.  
  414. Triplet *AllocTable( LONG Colors, LONG Dep, LONG Offset )
  415. {
  416.     return 0L;
  417. }
  418.  
  419. UWORD Table1[] = { 0, 0x0FFF };
  420. UWORD Table2[] = { 0, 0x0E00, 0x00E0, 0x000E, 0x0E00, 0x00E0, 0x000E };
  421. WORD Table3[] = {
  422.     0,
  423.     0x0E00, 0x0770, 0x00E0, 0x0077, 0x000E, 0x0707, 0x0707, 0x0E00, 0x0770,
  424.     0x00E0, 0x0077, 0x000E, 0x0707, 0x0707 };
  425. UWORD Table4[] = {
  426.     0,
  427.     0x0E03, 0x0B06, 0x0909, 0x060B, 0x030E, 0x003E, 0x006B, 0x0099, 0x00B6,
  428.     0x00E3, 0x03E0, 0x06B0, 0x0990, 0x0B60, 0x0E30, 0x0E03, 0x0B06, 0x0909,
  429.     0x060B, 0x030E, 0x003E, 0x006B, 0x0099, 0x00B6, 0x00E3, 0x03E0, 0x06B0,
  430.     0x0990, 0x0B60, 0x0E30 };
  431. UWORD Table5[] = {
  432.     0,
  433.     0x0F20, 0x0E30, 0x0C50, 0x0B60, 0x0980, 0x0890, 0x06B0, 0x05C0, 0x03E0,
  434.     0x02F0, 0x00F2, 0x00E3, 0x00C5, 0x00B6, 0x0098, 0x006B, 0x005C, 0x003E,
  435.     0x002F, 0x020F, 0x030E, 0x050C, 0x060B, 0x0809, 0x0908, 0x0B06, 0x0C05,
  436.     0x0E03, 0x0F02, 0x0F00, 0x0F00, 0x0F20, 0x0E30, 0x0C50, 0x0B60, 0x0980,
  437.     0x0890, 0x06B0, 0x05C0, 0x03E0, 0x02F0, 0x00F2, 0x00E3, 0x00C5, 0x00B6,
  438.     0x0098, 0x006B, 0x005C, 0x003E, 0x002F, 0x020F, 0x030E, 0x050C, 0x060B,
  439.     0x0809, 0x0908, 0x0B06, 0x0C05, 0x0E03, 0x0F02, 0x0F00, 0x0F00 };
  440. UWORD Table6[] = {
  441.     0,
  442.     0x0F20, 0x0E30, 0x0C50, 0x0B60, 0x0980, 0x0890, 0x06B0, 0x05C0, 0x03E0,
  443.     0x02F0, 0x00F2, 0x00E3, 0x00C5, 0x00B6, 0x0098, 0x006B, 0x005C, 0x003E,
  444.     0x002F, 0x020F, 0x030E, 0x050C, 0x060B, 0x0809, 0x0908, 0x0B06, 0x0C05,
  445.     0x0E03, 0x0F02, 0x0F00, 0x0F00, 0x0F00, 0x0F20, 0x0E30, 0x0C50, 0x0B60,
  446.     0x0980, 0x0890, 0x06B0, 0x05C0, 0x03E0, 0x02F0, 0x00F2, 0x00E3, 0x00C5,
  447.     0x00B6, 0x0098, 0x006B, 0x005C, 0x003E, 0x002F, 0x020F, 0x030E, 0x050C,
  448.     0x060B, 0x0809, 0x0908, 0x0B06, 0x0C05, 0x0E03, 0x0F02, 0x0F00, 0x0F20,
  449.     0x0E30, 0x0C50, 0x0B60, 0x0980, 0x0890, 0x06B0, 0x05C0, 0x03E0, 0x02F0,
  450.     0x00F2, 0x00E3, 0x00C5, 0x00B6, 0x0098, 0x006B, 0x005C, 0x003E, 0x002F,
  451.     0x020F, 0x030E, 0x050C, 0x060B, 0x0809, 0x0908, 0x0B06, 0x0C05, 0x0E03,
  452.     0x0F02, 0x0F00, 0x0F00, 0x0F00, 0x0F20, 0x0E30, 0x0C50, 0x0B60, 0x0980,
  453.     0x0890, 0x06B0, 0x05C0, 0x03E0, 0x02F0, 0x00F2, 0x00E3, 0x00C5, 0x00B6,
  454.     0x0098, 0x006B, 0x005C, 0x003E, 0x002F, 0x020F, 0x030E, 0x050C, 0x060B,
  455.     0x0809, 0x0908, 0x0B06, 0x0C05, 0x0E03, 0x0F02, 0x0F00 };
  456. UWORD *TblPtr[] = { Table1, Table2, Table3, Table4, Table5, Table6 };
  457.  
  458. Triplet *SASM RainbowPalette( AREG(0) struct Screen *Screen,
  459.                              AREG(1) Triplet *Table, DREG(0) LONG Offset,
  460.                              DREG(1) LONG EP )
  461. {
  462.     struct Library *GfxBase;
  463.     static LONG i = 0L;
  464.     
  465.     if( Screen )
  466.     {
  467.         if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  468.         {
  469.             LONG Colors[] = { 2, 4, 8, 16, 32, 64 };
  470.             LONG Depth = Screen->BitMap.Depth;
  471.             UWORD *Cols, Tmp;
  472.     
  473.             Cols = &( TblPtr[Depth-1][i] );
  474.             Tmp = Cols[0];
  475.             Cols[0] = 0;
  476.             LoadRGB4(&( Screen->ViewPort ), Cols, Colors[Depth-1] );
  477.             Cols[0] = Tmp;
  478.             i = ++i % ( Colors[Depth-1] );
  479.         }
  480.         
  481.         if( GfxBase )
  482.             CloseLibrary( GfxBase );
  483.     }
  484.  
  485.     return 0L;
  486. }
  487.